package com.dap.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

/**
 * 
 * 打印日志参数
 * 
 */
public class PrintArgumentUtil {
    // 打印输入输出参数,需要排除的属性
    private static final Set<String> excludeProperties = new HashSet<>();
    private static SimplePropertyPreFilter simplePropertyPreFilter;


    private PrintArgumentUtil(String excludePropertiesStr) {
        PrintArgumentUtil.excludeProperties.addAll(Arrays.asList(excludePropertiesStr.split(",")));

        PrintArgumentUtil.simplePropertyPreFilter = new SimplePropertyPreFilter() {
            @Override
            public boolean apply(JSONSerializer serializer, Object source, String name) {
                return !excludeProperties.contains(name);
            }
        };
    }


    /**
     * 将参数对象转换成字符串，并且过滤指定的属性
     * 
     * @param object
     * @return
     */
    public static String toJSONString(Object object) {
        if (object == null)
            return "";

        if (object instanceof String) {
            return (String) object;
        }

        return JSON.toJSONString(object,
                PrintArgumentUtil.simplePropertyPreFilter,
                SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
    }
}
